Skip to content


ardupilot  None  ros2  dds  micro ros  xrce  sitl  plugin  SITL  debug  rangefinder  pymavlink  mavros  gazebo  distance sensor  system_time  timesync  cmake  gtest  ctest  101  cpp  c++  format  fmt  multithreading  spdlog  cyclonedds  eprosima  fastdds  simulation  config  ignition  bridge  sdf  tips  ign-transport  camera  sensors  lidar  aptly  apt  repository  encryption  pgp  docker  nvidia  git  bundle  submodules  github  hooks  pre-commit  lxd  container  lxc  x11  profile  vscode  marpit  presentation  marp  markdown  mermaid  mkdocs  video  ffmpeg  gstreamer  cheat-sheet  sdp  v4l2loopback  gi  kml  python  geo  snippets  cheat Sheet  asyncio  future  click  cli  numpy  project  template  black  isort  templates  cookiecutter  docs  project document  docstrings  flake8  linter  git-hook  mypy  unittest  pytest  pylint  from a-z  mock  iterator  generator  logging  tuple  namedtuple  typing  annotation  typever  pyzmq  zmq  msgpack  action  namespace  remap  control2  ros2_control  gdb  qos  tag  plugins  msg  node  zero-copy  shm  tutorial  algorithm  calibration  diff  pid  dev  colcon  colcon_cd  rpi  arm  qemu  settings  behavior  py_trees  bt  behavior_trees  blackboard  plot  visualization  debugging  diagnostic  DiagnosticTask  diagnostics  tutorials  gst  math  apm  rat_runtime_monitor  web  rosbridge  vue  binding  discovery  gazebo-classic  launch  spawn  model  cook  gps  imu  ray  gazebo_ros_ray_sensor  ultrsonic  range  ultrasonic  gazebo classic  wrench  effort  odom  ign  gz  xacro  ros_ign  diff_drive  odometry  joint_state  argument  OpaqueFunction  DeclareLaunchArgument  LaunchConfiguration  tmux  nav  slam  test  rclpy  goal abort  cancel goal  action client  action server  custom messages  executor  MultiThreadedExecutor  SingleThreadedExecutor  param  dynamic-reconfigure  service  client  setup.py  package.xml  parameter  parameters  custom  msgs  executers  pub  sub  rqt  rviz  rviz2  pose  marker  tf2  deb  package  setup  local_setup  rosdep  package manager  project settings  vcstool  urdf  robot_state_publisher  urdf_to_graphiz  joint  link  cross-compiler  nano  texture  tmuxp  loop device  rootfs  embedded  zah  linux  rm  ubuntu  sudo  sudoers  nopasswd  visudo  udev  ip  ss  network  netstat  snap  deploy  ssh  systemd  socat  networking  serial  udp  tc  mtu  select  robotics  kalman_filter  kalman  filter  control  extensions  json  yaml  schema  yocto  poky  projects  courses to follow  world  gazebo_ros2_control  position_controller  effort_controller  velocity_controller  gazebo_ros_force  gazebo_ros_joint_state_publisher  joint_state_publisher  vrx  buoyancy 

Python project template - pre-commit#

Demo of usage pre-commit tool

Install pre-commit tool and config it with four tools

  • isort
  • black
  • mypy
  • flake8

pre-commit#

  • Install pre-commit
  • Add config file
  • Install hooks
  • Add hooks
  • Run
install
python -m pip install pre-commit
config
touch .pre-commit-config.yaml
install hook
pre-commit install

hooks#

isort#

isort is a Python utility / library to sort imports alphabetically, and automatically separated into sections and by type.

  • config pre-commit
  • add isort config to pyproject.toml
  • install and run
.pre-commit-config.yaml
repos:
  - repo: https://github.com/PyCQA/isort
    rev: 5.12.0
    hooks:
      - id: isort
pyproject.toml
[settings]
[tool.black]
line-length = 120

[tool.isort]
profile = "black"
install and run
pre-commit install
# Run
pre-commit run -a

config with black

config isort with black profile


black#

Black is a PEP 8 compliant opinionated formatter. Black reformats entire files in place.

  • config pre-commit
  • add isort config to pyproject.toml
  • install and run
.pre-commit-config.yaml
repos:
  - repo: https://github.com/psf/black
    rev: 23.3.0
    hooks:
      - id: black
        language_version: python3.10
pyproject.toml
[settings]
[tool.black]
line-length = 120

[tool.isort]
profile = "black"
install and run
pre-commit install
# Run
pre-commit run -a

config isort

config isort with black profile

black and isort format import different, if both config as pre-commit action they interrupt each other

disable fmt format for code section

# fmt: off
...
# fmt: on

mypy#

  • config pre-commit
  • add mypy section to pyproject.toml config
  • install and run
.pre-commit-config.yaml
repos:
  - repo: https://github.com/pre-commit/mirrors-mypy
    rev: v0.942
    hooks:
      - id: mypy
pyproject.toml
[settings]
[tool.black]
line-length = 120

[tool.mypy]

add inline ignore

# type: ignore

flake8#

  • config pre-commit
  • add .flake8 config

code style checker for PEP8

.pre-commit-config.yaml
repos:
  - repo: https://github.com/PyCQA/flake8
    rev: 6.0.0
    hooks:
      - id: flake8
.flake8
[flake8]
max-line-length = 120
max-complexity = 10
exclude=src/apm_demos/test/*

Recap#

  • isort, black and mypy config from myproject.toml
  • flake8 config with its on file .flake8
  • isort must be config with black profile

disabled pre-commit

git commit --no-verify

.pre-commit-config.yaml#

files: src/apm_demos
repos:
  - repo: https://github.com/PyCQA/isort
    rev: 5.12.0
    hooks:
      - id: isort
  - repo: https://github.com/psf/black
    rev: 23.3.0
    hooks:
      - id: black
        language_version: python3.10
  - repo: https://github.com/pre-commit/mirrors-mypy
    rev: v0.942
    hooks:
      - id: mypy
  - repo: https://github.com/PyCQA/flake8
    rev: 6.0.0
    hooks:
      - id: flake8

Reference#